//
//  LCShowComposePictureView.swift
//  Swift0319
//
//  Created by admin on 2017/3/29.
//  Copyright © 2017年 __MyCompanyName__. All rights reserved.
//

import UIKit
import SVProgressHUD

private let reuseIdentifier = "Cell"
// 计算cell的间距
let showComposePictureViewitemMargin: CGFloat = 5
//计算cell的宽高
let showComposePictureViewCellWH = (ScreenWidth - 2 * showComposePictureViewitemMargin) / 4

class LCShowComposePictureView: UICollectionView {

    var imageList:Array<String> = Array()
    var closeEvent:(()->())?
    
    override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {

        let flowLayout : UICollectionViewFlowLayout = UICollectionViewFlowLayout()
        
        flowLayout.itemSize = CGSize(width: showComposePictureViewCellWH, height: showComposePictureViewCellWH)
        flowLayout.minimumLineSpacing = showComposePictureViewitemMargin
        flowLayout.minimumInteritemSpacing = showComposePictureViewitemMargin
        super.init(frame: frame, collectionViewLayout: flowLayout)

        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    //FIXME:- 上午10:29 设置视图
    ///设置视图
    private func setupUI(){
        backgroundColor = UIColor.white
        
        register(LCShowComposePictureViewCell.self, forCellWithReuseIdentifier: reuseIdentifier)
        dataSource = self
        
        delegate = self
    }
    override func layoutSubviews() {
        super.layoutSubviews()
        let flowLayout : UICollectionViewFlowLayout = UICollectionViewFlowLayout()
        let flowLayoutWidth: CGFloat = (self.bounds.size.width - 2 * showComposePictureViewitemMargin) / 3
        flowLayout.itemSize = CGSize(width: flowLayoutWidth, height: flowLayoutWidth)
        flowLayout.minimumLineSpacing = showComposePictureViewitemMargin
        flowLayout.minimumInteritemSpacing = 0
        self.collectionViewLayout = flowLayout
    }
}

extension LCShowComposePictureView: UICollectionViewDataSource,UICollectionViewDelegate{
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        let count = imageList.count
        return count
    }
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath) as! LCShowComposePictureViewCell
        let urlstr = URL(string: imageList[indexPath.item])
        cell.bgImageView.sd_setImage(with: urlstr, completed: nil)
        
        return cell
    }
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        collectionView.deselectItem(at: indexPath, animated: true)
        // 创建SDPhotoBrowser实例
        let browser = SDPhotoBrowser()
        // 原图的父控件
        browser.sourceImagesContainerView = self
        // 原图的数量
        browser.imageCount = imageList.count
        // 当前需要展示图片的index
        browser.currentImageIndex = indexPath.item
        // 设置代理
        browser.delegate = self
        // 展示图片浏览器
        browser.show()

    }
}
extension LCShowComposePictureView: SDPhotoBrowserDelegate{
    // 返回临时占位图片（即原来的小图）
    func photoBrowser(_ browser: SDPhotoBrowser!, placeholderImageFor index: Int) -> UIImage! {
        let cell = self.cellForItem(at:IndexPath(item: index, section: 0)) as! LCShowComposePictureViewCell
        return cell.bgImageView.image
    }
//    // 返回高质量图片的url
//    func photoBrowser(_ browser: SDPhotoBrowser!, highQualityImageURLFor index: Int) -> URL! {
//        let urlStr = imageList[index]
//        return URL(string: urlStr)
//    }

}
// 自定义LCShowComposePictureViewCell

class LCShowComposePictureViewCell: UICollectionViewCell {
    override init(frame: CGRect) {
        
        super.init(frame: frame)
        
        self.setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    //FIXME:- 上午10:29 设置视图
    ///设置视图
    private func setupUI(){
        contentView.addSubview(bgImageView)
        bgImageView.snp.makeConstraints { (make) in
            make.edges.equalTo(contentView)
        }
    }
    //FIXME:- 懒加载控件
    lazy var bgImageView:UIImageView = {
        let imageView : UIImageView = UIImageView()
        imageView.image = UIImage.init(named: "祷告见证默认图")
        imageView.layer.masksToBounds = true
        imageView.contentMode = .scaleAspectFill;
        return imageView
    }()
    
    
}
